package com.yenn.barcode.otherdemo;

import com.swetake.util.Qrcode;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * #### 6-4 使用QR Code方式生成和解析二维码 (16:14)
 * QRCode
 * 1. 生成: http://www.swetake.com/qrcode/index.html
 * 2. 读取: https://osdn.jp/projects/qrcode/
 */
public class CreateQRCode2 {
    public static void main(String[] args) {
        int width=67 + 12*(7-1);
        int height = 67 + 12*(7-1);

        Qrcode x = new Qrcode();
        x.setQrcodeErrorCorrect('M'); //纠错等级
        x.setQrcodeEncodeMode('B');// N代表数字，A代表a-Z，B代表其他字符
        x.setQrcodeVersion(7);

        String qrData = "www.imooc.com";
        byte[] d = new byte[0];
        try {
            d = qrData.getBytes("GB2312");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }


        // 画图
        BufferedImage bufferedImage = new BufferedImage(width ,height,BufferedImage.TYPE_INT_RGB) ;
        Graphics2D gs = bufferedImage.createGraphics();
        gs.setBackground(Color.WHITE);
        gs.setColor(Color.BLACK);
        gs.clearRect(0,0,width,height);
        int pixoff = 2;//偏移量

        if (d.length>0&&d.length<120){
            boolean[][] s = x.calQrcode(d);
            for (int i = 0; i < s.length; i++) {
                for (int j = 0; j < s.length; j++) {
                    if (s[j][i]){
                        gs.fillRect(j*3+pixoff,i*3+pixoff,3,3);
                    }
                }
            }
        }
        gs.dispose();
        bufferedImage.flush();
        try {
            ImageIO.write(bufferedImage,"png",new File("D:"+File.separator+"qrcode2.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
